$(function(){
 /*$(document).on('click','.upload img',function(){
    var piHeight = $('.popup_img').height();
    var piWidth = $('.popup_img').width();
    var piTop = -(piHeight/2)+'px';
    var piLeft = ((1024-piWidth)/2)+'px';
    $('.bg_black').fadeIn();
    $('.popup_img').fadeIn().css('margin-top',piTop);
    $('.popup_img').fadeIn().css('left',piLeft);
    $('.popup_img_icon').fadeIn();
  });
  $(document).on('click','.bg_black',function(){
    $('.bg_black').fadeOut();
    $('.popup_img').fadeOut();
    $('.popup_img_icon').fadeOut();
  });
  $(document).on('click','.popup_img_icon',function(){
    $('.bg_black').fadeOut();
    $('.popup_img').fadeOut();
    $('.popup_img_icon').fadeOut();
  });*/
  
padMarketing.ajaxNormal("/pad_marketing/drdocAction/getPending.action", {},
		function(data) {
			console.log(data)
			var html = '';
			for(var i=0;i<data.length;i++){
				html+='<div class="panel panel-default" data=\''+JSON.stringify(data[i])+'\'>';
				html+='<div class="panel-heading">';
				html+='<div class="form-group no-margin">';
				html+='<label class="col-md-2 control-label no-margin">保单号</label>';
				html+='<div class="col-md-4"><p class="form-control-static blue">'+data[i].policyRef+'</p></div>';
				html+='<label class="col-md-3 control-label no-margin">照片类型</label>';
				html+='<div class="col-md-3"><p class="form-control-static blue">'+data[i].itemTypeName+'</p></div>';
				html+='<div class="clearfix"></div>';
				html+='</div>';
				html+='</div>';
				html+='<div class="panel-body">';
				html+='<div class="form-group no-margin">';
				html+='<label class="col-md-2 control-label">投保单号</label>';
				html+='<div class="col-md-4"><p class="form-control-static blue">'+data[i].preprintedNo+'</p></div>';
				html+='<label class="col-md-3 control-label">生效日</label>';
				html+='<div class="col-md-3"><p class="form-control-static blue">'+data[i].inceptionDate+'</p></div>';
				html+='<div class="clearfix"></div>';
				html+='<label class="col-md-2 control-label">投保人</label>';
				html+='<div class="col-md-4"><p class="form-control-static blue">'+data[i].phName+'</p></div>';
				html+='<label class="col-md-3 control-label">被保险人</label>';
				html+='<div class="col-md-3"><p class="form-control-static blue">'+data[i].insNames+'</p></div>';
				html+='<div class="clearfix"></div>';
				html+='<label class="col-md-2 control-label">主险名称</label>';
				html+='<div class="col-md-4"><p class="form-control-static blue">'+data[i].productName+'</p></div>';
				html+='<label class="col-md-3 control-label">重传原因</label>';
				html+='<div class="col-md-3"><p class="form-control-static blue">'+$.trim(data[i].rejectReason)+'</p></div>';
				html+='<div class="clearfix"></div>';
				html+='<label class="col-md-2 control-label">上传照片</label>';
				html+='<div class="col-md-4">';
				html+='<div class="upload uploadimgs">';
				html+='<img src=""  id="uploadimg'+i+'"  onclick="check(this)">';
				//html+='<img src=""  id="uploadimg'+i+'">';
				//html+='<input accept="image" type="file" style="width:100%;height:100%;opacity:0" class="uploadfile" id="uploadfile'+i+'" style="z-index:1">';
				html+='</div>';
				html+='</div>';
				html+='<div class="clearfix"></div>';
				html+='<div class="col-md-2 col-md-offset-5">';
				html+='<button class="btn btn-block btn-primary uploadbtn">上传</button>';
				html+='</div>';
				html+='</div>';
				html+='</div>';
				html+='</div>';			
			}
			$("#panel").append(html);
			
			/*$(".uploadfile").change(function() {			
				var m=$(this).attr("id").substr(-1);
				var objUrl = getObjectURL(this.files[0]);
				if (objUrl) {
					$('#uploadimg'+m).attr("src", objUrl);
					//var img=document.getElementById("uploadimg"+m);
					//img.src=objUrl;
					var pars=JSON.parse($('#uploadimg'+m).parent().parent().parent().parent().parent().attr("data"));	
					console.log(pars)
					upload(pars.contractId,pars.policyRef,pars.itemType,objUrl);
				}				
			});	*/
	
			 if(myScroll){
			   		myScroll.refresh(); 
			   	 }		
			
		}, function(data) {
			
		})  	
})

function check(img){
	var m=$(img).attr("id");
	$("#panel").data("mun",m);
	library();
}
function library() {
	openLibrary("uplimg");
}
//上传图片
function uplimg(src) {
	var m1=$("#panel").data("mun");
	var img = document.getElementById(m1);
	var pars=JSON.parse($("#"+m1).parent().parent().parent().parent().parent().attr("data"));
	src = 'data:image/jpg;base64,' + src;
	img.src = src;
	//var imgsrc=$("#"+m1).attr("src");
	// $(m1).attr('src',src);
	upload(pars,src);
}

$(document).on("click",".uploadbtn",function(){
			var dom=$(this).parent().parent().find(".uploadimgs img").attr("src");
			var par=JSON.parse($(this).parent().parent().parent().parent().attr("data"));
			var index=$(this).parent().parent().parent().parent().index();
			//var after={"contractId":par.contractId,"itemType":par.itemType,"sign":par.sign};
			var after={"contractId":par.contractId,"policyRef":par.policyRef,"preprintedNo":par.preprintedNo,"itemType":par.itemType,"sign":par.sign};
			//var after={"contractId":par.contractId,"policyRef":par.policyRef,"preprintedNo":par.preprintedNo,"itemType":par.itemType,"sign":par.sign};
			//upload(par,dom);
			//console.log(par);
			//var par={"contractId":4695715,"itemType":"D","sign":"CB06EEB28B110154B3ABAF93A6F80255"};
			if(!dom){
					alertMsg("没有添加图片");	
				}
			else{
				padMarketing.ajaxNormal("/pad_marketing/drdocAction/afterUpload.action", after,
						function(data) {				
							$(".panel").eq(index).remove();
							alertMsg("上传成功");
							 if(myScroll){
							   		myScroll.refresh(); 
							   	 }		
						},
						function(data) {
							alertMsg("上传失败");
						}
				)
			}
	})

//input上传
function getObjectURL(file) {
	var url = null;
	if (window.createObjectURL != undefined) { // basic
		url = window.createObjectURL(file);
	} else if (window.URL != undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file);
	} else if (window.webkitURL != undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file);
	}
	return url;
}

//上传图片
function upload(data,image){	
	
	$.ajax({
		type:"post",
		url:"/pad_marketing/drdocAction/uploadImg.action",
		data:{'jsonParam':JSON.stringify(data),'image':image},
		dataType:'json',
		success:function(data){
			//alertMsg("上传成功");
			//console.log(data)
		},
		error:function(data){
			//alertMsg(data.responseText);
		}
	})
/*	var af={"policyRef":policyRef,"preprintedNo":preprintedNo,"itemType":itemType,"image":image};
	padMarketing.ajaxNormal("/pad_marketing/drdocAction/uploadImg.action", af,
			function(data) {				
				alertMsg("ajax成功");
			},
			function(data) {
				alertMsg(data.responseText);
			}
	)*/
}


